package com.tess.tboot.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class TbootServiceImpl<M extends TbootBaseMapper<T>, T> implements TbootBaseService<T> {

    @Autowired
    protected M baseMapper;

    @Override
    public TbootBaseMapper<T> getMapper() {
        return baseMapper;
    }

    /**
     * 根据ID获取
     *
     * @param id
     * @return
     */
    @Override
    public T get(Serializable id) {
        return baseMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取所有列表
     *
     * @return
     */
    @Override
    public List<T> getAll() {
        return baseMapper.selectAll();
    }

    /**
     * 获取总数
     *
     * @param entity
     * @return
     */
    @Override
    public int getTotalCount(T entity) {
        return baseMapper.selectCount(entity);
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @Override
    public T save(T entity) {
        baseMapper.insert(entity);
        return entity;
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Override
    public int update(T entity) {
        return baseMapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 批量保存与修改
     *
     * @param entities
     * @return
     */
    @Override
    public int saveAll(List<T> entities) {
        return baseMapper.insertList(entities);
    }

    /**
     * 删除
     *
     * @param entity
     */
    @Override
    public void deleteEntity(T entity) {
        baseMapper.delete(entity);
    }

    /**
     * 根据Id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id) {
        baseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     *
     * @param entities
     */
    @Override
    public void delete(Iterable<T> entities) {
        for (T entity : entities) {
            baseMapper.delete(entity);
        }
    }

    /**
     * 清空缓存，提交持久化
     */
    @Override
    public void flush() {

    }

    /**
     * 获取查询条件的结果数
     *
     * @param params
     * @return
     */
    @Override
    public long count(Map<String, Object> params) {
        return 0;
    }

    /**
     * 根据条件查询获取
     *
     * @param params
     * @return
     */
    @Override
    public List<T> findAll(Map<String, Object> params) {
        return null;
    }

    /**
     * 根据条件查询获取
     *
     * @param pageable
     * @param params
     * @return
     */
    @Override
    public Page<T> findWithPage(Map<String, Object> params, Pageable pageable) {

        //baseMapper.findxxxx
        return null;
    }
}
